package com.hellogood.yaohuo.client.common;

import java.util.List;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.hellogood.yaohuo.shared.services.QyOrgService;
import com.hellogood.yaohuo.shared.vo.QuyuVo;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.TreeModelType;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.form.fields.HiddenItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.grid.events.SelectionChangedHandler;
import com.smartgwt.client.widgets.grid.events.SelectionEvent;
import com.smartgwt.client.widgets.tree.Tree;
import com.smartgwt.client.widgets.tree.TreeGrid;
import com.smartgwt.client.widgets.tree.TreeGridField;
import com.smartgwt.client.widgets.tree.TreeNode;

public class QuyuTreeWindow extends Window {
	private TreeNode[] treeNodes = null;

	private TextItem textItem = null;
	private HiddenItem levelItem = null;
	private TreeGrid treeGrid = null;

	public QuyuTreeWindow(TextItem textItem, HiddenItem levelItem) {
		this.textItem = textItem;
		this.levelItem = levelItem;
		initData();
		initView();
	}

	private void initData() {
		String staffCode = LoginUser.getInstance().getCurrentUser().getStaffCode();
		QyOrgService.Util.getInstance().getQuyuOrgByStaffCode(staffCode, new QuyuCodeAsynCallback());

	}

	private void initView() {
		TreeGridField nodeIdField = new TreeGridField("nodeId");
		nodeIdField.setHidden(true);
		TreeGridField nameField = new TreeGridField("name", "区域");
		treeGrid = new TreeGrid();
		treeGrid.setFields(nodeIdField, nameField);
		treeGrid.setWidth("90%");
		treeGrid.setHeight("90%");
		treeGrid.setAlign(Alignment.CENTER);
		treeGrid.addSelectionChangedHandler(new TreeGridSelectionHandler());
		treeGrid.setTop(40);
		treeGrid.setLeft(22);
		addChild(treeGrid);
	}

	private class TreeGridSelectionHandler implements SelectionChangedHandler {

		@Override
		public void onSelectionChanged(SelectionEvent event) {
			ListGridRecord record = treeGrid.getSelectedRecord();
			if (record != null) {
				String parentId = record.getAttribute("parentId");
				if (parentId.equals("0")) {
					levelItem.setValue("2");
				} else {
					levelItem.setValue("3");
				}
				textItem.setValue(record.getAttribute("code") + "-" + record.getAttribute("name"));
				QuyuTreeWindow.this.setVisible(false);
			}
		}
	}

	private class QuyuCodeAsynCallback implements AsyncCallback<List<QuyuVo>> {

		@Override
		public void onFailure(Throwable caught) {
			SC.say(caught.getMessage());
		}

		@Override
		public void onSuccess(List<QuyuVo> result) {
			if (result != null) {
				treeNodes = new TreeNode[result.size()];
				for (int i = 0; i < result.size(); i++) {
					QuyuVo vo = result.get(i);
					TreeNode r = new TreeNode();
					r.setAttribute("nodeId", vo.getCode());
					r.setAttribute("code", vo.getCode());
					r.setAttribute("name", vo.getName());
					r.setAttribute("isOpen", true);
					r.setAttribute("parentId", vo.getParentCode());
					treeNodes[i] = r;
				}

				Tree tree = new Tree();
				tree.setModelType(TreeModelType.PARENT);
				tree.setNameProperty("name");
				tree.setRootValue("0");
				tree.setIdField("nodeId");
				tree.setParentIdField("parentId");
				tree.setOpenProperty("isOpen");
				tree.setData(treeNodes);

				treeGrid.setData(tree);
			}
		}
	}
}
